import util from "../../common/js/util.js";
import getbooklistbytimeData from '/static/json/getbooklistbytime.json';
var app = getApp();
const updateManager = dd.getUpdateManager()
import Qs from "qs";
//requedBooks:getbooklistbytimeData.data

Page({
  data: {
    requedBooks: [],
    expandBooks: [],
    page:1,
    bookName:"",
    scrollTop: 0,
    toView: "",
    showLoadMoreTxt: "over",
    sysNoteList:[],//系统消息提示列表

  },
  saveRef(ref) {
    //调用组件方法
    // ref.testFn()
  },

  onLoad() {


    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      // // res.hasUpdate是否有更新
      //util.message(res.hasUpdate + "")

      if (res.hasUpdate) {
        updateManager.onUpdateReady(function (ret) {
          //console.log(ret.version) // 更新版本号
          dd.confirm({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
      }
    })

    // if (!app.prototype.$Token) util.getUserMessage();
    this.getIsLogin();

  },
  // onShow(){
  //   this.getIsLogin();

  // },
  initFn(){
    this.searchFn();
    this.getSysNoteList();
    // this.getBookList(2);

  },
  searchFn(e){
    this.setData({
      bookName:e,
      page:1,
      requedBooks:[]
    })
    this.getBookList()
  },
  onChange(e){
   // console.log("--onChange",e);
    // this.setData({
    //   bookName:e
    // })
    this.searchFn(e);
  },
  onConfirm(e){
    // this.setData({
    //   bookName:e
    // })
    this.searchFn(e);    
  },
  getBookList(){

    // /list   /allList
    let api = "/user/book/pageList", list = { 
      page:this.data.page,
      pageSize:7,
      bookName:this.data.bookName,
      state:1
     };
    
    dd.showLoading({
      content: '数据加载中...',
    })
    this.setData({
      showLoadMoreTxt: "load"
    });
    let dataList=this.data.requedBooks;

    app.$post(api, list).then(res => {
      dd.hideLoading();
      
      if (res.code == 200) {
        this.setData({
          showLoadMoreTxt: "over"
        })
        let page=this.data.page;

        
        let arr=res.data.list;
        if(util.isNotEmpty(arr)){
          let arr_list=dataList.concat(arr);
          page=page+1;

          this.setData({
            requedBooks:arr_list,
            totalResult: res.data.totalResult,
            showLoadMoreTxt: "over",
            page
          })
        }
        
        // if(type==2){
        //   this.setData({
        //     expandBooks:res.data
        //   })
        // }
        dd.stopPullDownRefresh();
      }else{
        
      }
    }).catch(function(error) {
      console.log("---error");
      console.log(error)
    });
  },
  getIsLogin(){
    let api = "/user/info/getIsLogin", list = {  };
    app.$post(api, list).then(re => {
      try {
        if (re.code == 401) {
          app.authLoginFn().then(res=>{
           // console.log("--res:",res);
            this.initFn();

          })
        }
        else {
          this.initFn();
        };
      } catch (error) {
        
      }

    }).catch(err => {
      
      
    });
  },
  getSysNoteList(){
    let api = "/common/getSysNoteList", list = {  };
    app.$post(api, list).then(re => {
      try {
        if (re.code == 200) {
          this.setData({
            sysNoteList:re.data
          })
        }
        else {
          this.initFn();
        };
      } catch (error) {
        
      }

    })
  },
  onShareAppMessage() {

    return {
      title: '恒丽书城',
      desc: '恒丽书城',
      path: 'pages/home/home',
    };
  },
  onScrollToLower() {
  //  console.log("---开始触底");
    this.getBookList();
  },
  onPullDownRefresh() {
    // 页面被下拉
  //  console.log("---页面被下拉");
  //  dd.stopPullDownRefresh();
    this.searchFn();
  },
  scroll(e) {
    this.setData({
      scrollTop: e.detail.scrollTop,
      toView: ""
    })
  },

  handlePath(e){
    let url=e.target.dataset.path;
    dd.navigateTo({
      url
    })
  },
  onTapFabClick(e) {
    this.setData({
      scrollTop: 0,
      toView: "top"
    })
  },
});
